<%-- 
    Document   : commission
    Created on : Sep 15, 2011, 7:55:13 PM
    Author     : bi
--%>

<%@page import="BussinessLayer.MembersFactory"%>
<%@page import="DTO.Member.Members"%>
<%@page import="Control.Controler"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">

<html>
    <head>

        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link rel="stylesheet" type="text/css" href="css/main.css" />
        <link rel="stylesheet" type="text/css" href="css/commission.css" />
        <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
        <%@taglib uri="http://java.sun.com/jsp/jstl/xml" prefix="x" %>
        <c:set var="chkCurMember" value="${sessionScope.USER}"/>
        <c:if test="${empty chkCurMember}">
            <c:redirect url="index.jsp"/>
        </c:if>
        <script type="text/javascript">
            function autodate() {
                document.getElementById("searchTxt").setAttribute(value, 'YYYY-MM-DD');
            }
            
            //var auto = self.setInterval("autoGenXml()", 3000);

            function autoGenXml(){
            <%
                        String realPath = getServletContext().getRealPath("/") + "WEB-INF/";
                        Members mem = new Members();
                        try {
                            mem = (Members) session.getAttribute("USER");

                            MembersFactory memFac = new MembersFactory();
                            memFac.genXmlFromDB(mem, realPath);


                        } catch (Exception ex) {
                        }
            %>
                }

                var monthValidate = function() {

                    var phone = document.getElementsByName("searchText")[0].value;
                    var ccb = document.getElementsByName("searchOption")[0].value;
                    if (ccb == "date"){
                        if(phone && !(new RegExp("^(0[1-9])|(1[0-2])$")).test(phone)) {
                            document.getElementById("lblPhone").innerHTML = "Month is invalid!";
                            //document.getElementById("btnAdd").disabled = true;
                            return false;
                        }
                    }
                    document.getElementById("lblPhone").innerHTML = "";
                    return true;
                }
        </script>
        <title>Commission</title>
    </head>
    <body class="default" onload="self.setInterval(autoGenXml(), 5*1000);">
        <div class="logo">MULTI LEVEL MANAGEMENT</div><br/>
        <form action="Controler" method="POST">
            <table class="buttongroups">
                <tr>
                    <td>
                        <button class="good" type="submit" name="btnAction" value="view"
                                onclick="location.href='viewtree.jsp'">View Tree</button>
                    </td>
                    <td>
                        <button class="quite" type="submit" name="btnAction" value="commission"
                                onclick="location.href='commission.jsp'" >Commission</button>
                    </td>
                    <td>
                        <button class="average" type="submit" name="btnAction" value="add"
                                onclick="location.href='addnew.jsp'">Add New</button>
                    </td>
                    <td>
                        <button class="bad" type="submit" name="btnAction" value="logout">Log Out</button>
                    </td>
                </tr>
            </table>
        </form>
        <div class="workarea">
            <c:import var="xml" url="${sessionScope.REPORTURI}"/>
            <x:parse var="doc" xml="${xml}" scope="session"/>
            <c:import var="xmlMem" url="${sessionScope.MEMBERURI}"/>
            <x:parse var="docMem" xml="${xmlMem}" scope="session"/>
            <%--
        <c:import var="xsl" url="WEB-INF/reports.xsl"/>
            <x:transform xml="${xml}" xslt="${xsl}"/>
            --%>
            <form action="commission.jsp" method="POST">
                <table class="searchgroups" align="right">
                    <tr>

                        <td><label id="lblPhone" style="color: white;"/> Filter by:</td>
                        <td>
                            <select name="searchOption">
                                <option value="nothing">Nothing</option>
                                <option value="id">ID</option>
                                <option value="date" onclick="autodate()">Month</option>
                                <option value="sender">Sender</option>
                                <option value="amount">Amount</option>
                            </select>
                        </td>
                        <td>
                            <input id="searchTxt" class="textbox" type="text" name="searchText" value="" onblur="monthValidate()"/>

                        </td>
                        <td>
                            <button class="good" type="submit" name="search">Search</button>
                        </td>
                    </tr>
                </table>
            </form>
            <br/>
            <c:set var="searchData" value="${param.searchText}"/>
            <c:set var="searchOpt" value="${param.searchOption}"/>
            <table class="commission" border="0" cellpadding="0" cellspacing="0">
                <tr>
                    <th>
                        ID
                    </th>
                    <th>
                        Sender
                    </th>
                    <th>
                        Receiver
                    </th>
                    <th>
                        Commission Amount
                    </th>
                    <th>
                        Date
                    </th>
                </tr>
                <%--
                <c:if test="${not empty month and not empty year}">
                    <x:forEach var="row" select="$doc//Report[translate(normalize-space(Date), '/', '')>=translate('$year/$month/01', '/', '') and translate(normalize-space(Date), '/', '')<=translate('$year/$month/31', '/', '')]">
                        <tr class="change">
                            <td>
                                <x:out select="$row/ReportId"/>
                            </td>
                            <td>
                                <x:out select="$row/MemId_Send"/>
                            </td>
                            <td>
                                <x:out select="$row/MemId_Rev"/>
                            </td>
                            <td>
                                <x:out select="$row/Commission"/>
                            </td>
                            <td>
                                <x:out select="$row/Date"/>
                            </td>
                            <td>
                                <x:out select="$row/Note"/>
                            </td>
                        </tr>
                    </x:forEach>
                </c:if>
                --%>
                <x:forEach var="row" select="$doc//*[local-name()='report']">
                    <x:set var="memSend" select="$row/*[local-name()='MemId_Send']"/>
                    <x:set var="memRev" select="$row/*[local-name()='MemId_Rev']"/>
                    <c:set var="display" value="0"/>
                    <c:choose>
                        <c:when test="${searchOpt == 'id'}">
                            <x:if select="$row/*[local-name()='ReportId'] = $searchData">
                                <c:set var="display" value="1"/>
                            </x:if>
                        </c:when>
                        <c:when test="${searchOpt == 'sender'}">
                            <x:if select="$row//*[local-name()='FullName_Send' and contains(text(),$searchData)]">
                                <c:set var="display" value="1"/>
                            </x:if>

                            <%--
                            <x:if select="$docMem//*[local-name()='member' and MemId=$memSend and contains(FullName,$searchData)]">
                                <c:set var="display" value="1"/>
                            </x:if>
                            --%>
                        </c:when>
                        <c:when test="${searchOpt == 'date'}">
                            <x:if select="$row//*[local-name()='Date' and substring(text(),6,2)=$searchData]">
                                <c:set var="display" value="1"/>
                            </x:if>
                        </c:when>
                        <c:when test="${searchOpt == 'amount'}">
                            <x:if select="$row/*[local-name()='Commission'] = $searchData">
                                <c:set var="display" value="1"/>
                            </x:if>
                        </c:when>
                        <c:otherwise>
                            <c:set var="display" value="1"/>
                        </c:otherwise>
                    </c:choose>
                    <%--
                    <tr>
                        <td colspan="5">
                            <c:out value="${searchOpt}"/>-
                            <c:out value="${searchData}"/>-
                            <c:out value="${display}"/>
                        </td>
                    </tr>
                    --%>
                    <c:if test="${display=='1'}">
                        <tr class="change">
                            <td>
                                <x:out select="$row/*[local-name()='ReportId']"/>
                            </td>
                            <td>
                                <x:out select="$row//*[local-name()='FullName_Send']"/>
                                <%--
                                <x:out select="$docMem//*[local-name()='FullName' and (../*[local-name()='MemId']=$memSend)]"/>
                                --%>
                            </td>
                            <td>
                                <x:out select="$row//*[local-name()='FullName_Rev']"/>
                                <%--
                                <x:out select="$docMem//*[local-name()='FullName' and (../*[local-name()='MemId']=$memRev)]"/>
                                --%>
                            </td>
                            <td>
                                <x:out select="$row/*[local-name()='Commission']"/>
                            </td>
                            <td>
                                <x:out select="$row/*[local-name()='Date']"/>
                            </td>
                        </tr>
                    </c:if>
                </x:forEach>
            </table>


            <%--
            <table class="commission" border="1">
                <tr>
                    <th>
                        Full Name
                    </th>
                    <th>
                        Phone
                    </th>
                    <th>
                        Email
                    </th>
                    <th>
                        Address
                    </th>
                    <th>
                        Register Date
                    </th>
                    <th>
                        Available Score
                    </th>
                    <th>
                        Potential Score
                    </th>
                    <th>
                        Original Score
                    </th>
                    <th>
                        Note
                    </th>
                </tr>
                <x:forEach var="row" select="$doc//Member">
                    <tr class="change">
                        <td>
                            <x:out select="$row/FullName"/>
                        </td>
                        <td>
                            <x:out select="$row/Phone"/>
                        </td>
                        <td>
                            <x:out select="$row/Email"/>
                        </td>
                        <td>
                            <x:out select="$row/Address"/>
                        </td>
                        <td>
                            <x:out select="$row/DateReg"/>
                        </td>
                        <td>
                            <x:out select="$row/AvaiableScore"/>
                        </td>
                        <td>
                            <x:out select="$row/HideScore"/>
                        </td>
                        <td>
                            <x:out select="$row/OriginalScore"/>
                        </td>
                        <td>
                            <x:out select="$row/Note"/>
                        </td>
                    </tr>
                </x:forEach>
            </table>
            --%>
        </div>
    </body>
</html>
